package TUILayer;
import java.util.Scanner;
import ControlLayer.LoanCtr;
/**
 * @author (DM80 Group 5, Anders, Michael, Jesper, Lars og Kenni) 
 * @version (16/11-2012)                 
 */

public class LoanUI
{
    private LoanCtr loanCtr;//DVD UI must know the DVD controller 
    
    public LoanUI()
    {
        loanCtr = new LoanCtr();    
    }
    
    public void start(LoanCtr ctr)
    {
        loanCtr= ctr;
        loanMenu();
    }
    
    public void start()
    {
        loanMenu(); 
    }
    
    public void loanMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choise = writeDVDMenu();
            if (choise == 1)
            {
                createLoan();
            }
            else{
                if(choise == 2){
                   returnLoan();
                }
                else{
                    if(choise == 3){
                        listAllLoans();
                    }
                    else{
                        exit = true;
                    }//end else
                }//end else
            }//end else
            
        }//end while
    }
    private int writeDVDMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Loan Menu ***");
            System.out.println(" (1) Create Loan");
            System.out.println(" (2) Return DVD");
            System.out.println(" (3) List All Loans");
            System.out.println(" (4) Return");
            System.out.print("\n Make your choise: ");
            
            int choise = keyboard.nextInt();
            return choise;
    }
    
    private int inputLoanNr()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type Loan number: ");
        int no = keyboard.nextInt();
        return no;
    }
    
    private String inputLoanDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type Loan Date: ");
        String loanDate = keyboard.nextLine();
        return loanDate;
    }
    
    private String inputLoanPeriod()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type Loan Period: ");
        String loanNr = keyboard.nextLine();
        return loanNr;
    }
    
    private String inputLoanStatus()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type Loan Status: " );
        String loanStatus = keyboard.nextLine();
        return loanStatus;
    }
    
    private int inputPersonId()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type Person Id: " );
        int personId = keyboard.nextInt();
        return personId;
    }
    
    private int inputDvdId()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type dvd id to get samples: " );
        int dvdNr = keyboard.nextInt();
        return dvdNr;
    }
    
    private int inputSampleSerialNr()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type Sample Serial Nr: " );
        int serialNr = keyboard.nextInt();
        return serialNr;
    }
    
    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }   
    
    private void createLoan()
    {
        int _loanId = inputLoanNr();
        String _loanDate = inputLoanDate();
        String _loanPeriod = inputLoanPeriod();
        String _loanStatus = inputLoanStatus();
        
        boolean _personFound = false;
        int _personId;
        _personId = 0;
        
        while(!_personFound)
        {
            _personId = inputPersonId();
            
            String personInfo = loanCtr.getPersonInfo(_personId);
            
            if(personInfo.isEmpty())
            {
                System.out.println(" Could not find Person id try another!");
            }
            else
            {
                _personFound = true;
                System.out.println(personInfo);
            }
        }
        
        boolean _dvdFound = false;
        int _dvdId;
        _dvdId = 0;
        
        while(!_dvdFound)
        {
            _dvdId = inputDvdId();
            
            String dvdInfo = loanCtr.getDvdInfo(_dvdId);
            
            if(dvdInfo.isEmpty())
            {
                System.out.println(" Could not find Person id try another!");
            }
            else
            {
                _dvdFound = true;
                System.out.println(dvdInfo);
            }
        }
        
        int _sampleId = inputSampleSerialNr();
        
        boolean success = loanCtr.createLoan(_loanId, _loanDate, _loanPeriod, _loanStatus, _personId, _dvdId, _sampleId);
        
        System.out.println();
        
        if(success)
        {
            System.out.println("New loan has been created!");
        }
        else
        {
            System.out.println("New loan could not be created!");   
        }
        
        pause();
    }
    
    private void listAllLoans()
    {
        String loans = loanCtr.listAllLoans();
        
        if(loans.isEmpty())
        {
            System.out.println("List of loans is empty!");
        }
        else
        {
            System.out.println(loans);
        }
        
        pause();
    }
    
    private void returnLoan()
    {
        int _loanId = inputLoanNr();
        
        if(loanCtr.returnDvd(_loanId))
        {
            System.out.println("Your dvd is now returned!");
        }
        else
        {
            System.out.println("Could not find the loan!");
        }
    }
}



